using System;
using System.ComponentModel;
using System.Windows.Forms;
using InfoExpress.HIV.WinApp.Utils;
using System.Data;
using InfoExpress.WinApp.Utils;
using InfoExpress.HIV.Domains;
using InfoExpress.HIV.Services;
using System.Drawing;

namespace InfoExpress.HIV.WinApp.Forms
{
  public partial class FrmDsDuyetCapThe : DevExpress.XtraEditors.XtraForm
  {
    private DataTable _dtbDsDuyetCapThe;

    public FrmDsDuyetCapThe()
    {
      InitializeComponent();
    }

    #region Event Handlers

    private void FrmDsDuyetCapThe_Load(object sender, EventArgs e)
    {
      LoadData();
    }

    #region Command Handlers

    private void btnOK_Click(object sender, EventArgs e)
    {
      Cursor = Cursors.WaitCursor;
      MainFormUtils.StartProgress();
      MainFormUtils.SetSavingStatus();
      bwDataSaving.RunWorkerAsync();
    }

    private void btnPrint_Click(object sender, EventArgs e)
    {

    }

    #endregion

    #region Data Loading

    private void bwDataLoading_DoWork(object sender, DoWorkEventArgs e)
    {
      try
      {
        DataSet dtsDsBenhNhan = GlobalCache.BenhNhanDangKyService.DsBenhNhanChuaDuyet();
        _dtbDsDuyetCapThe = dtsDsBenhNhan.Tables[0];
      }
      catch (Exception ex)
      {
        _dtbDsDuyetCapThe = null;
        Logger.LogCritical("FrmDsDuyetCapThe::bwDataLoading_DoWork", ex, "General", null);
      }
    }

    private void bwDataLoading_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      if (_dtbDsDuyetCapThe != null)
      {
        gcDsBenhNhan.DataSource = _dtbDsDuyetCapThe;
      }
      else
      {
        UIUtils.ShowCannotLoadDataMessage();
      }

      Cursor = Cursors.Default;
      MainFormUtils.StopProgress();
      MainFormUtils.SetLoadedStatus();
    }

    #endregion DataLoading BackgroundWorker

    #region Data Saving

    private void bwDataSaving_DoWork(object sender, DoWorkEventArgs e)
    {
      try
      {
        DataTable dtbDsDuyetCapThe = (DataTable)gcDsBenhNhan.DataSource;

        foreach (DataRow dtrDuyetCapThe in dtbDsDuyetCapThe.Rows)
        {
          BenhNhanDangKy benhNhan =
            GlobalCache.BenhNhanDangKyService.GetByBenhNhanId((int)dtrDuyetCapThe["BenhNhanId"]);

          //Duyet & Khong Duyet
          if ((int)dtrDuyetCapThe["Duyet"] == 2 || (int)dtrDuyetCapThe["Duyet"] == 3)
          {
            benhNhan.TrangThaiId = (int)dtrDuyetCapThe["Duyet"];
            benhNhan.GhiChuDuyet = dtrDuyetCapThe["GhiChu"] as string;
            benhNhan.UserDuyet = GlobalCache.Permission.Username;
            benhNhan.NgayDuyet = DateTime.Today;
          }

          GlobalCache.BenhNhanDangKyService.Save(benhNhan);
          e.Result = true;
        }

        //For Non-Webservice
        //using (ConnectionScope.CreateTransaction())
        //{
        //  foreach (DataRow dtrDuyetCapThe in dtbDsDuyetCapThe.Rows)
        //  {
        //    BenhNhanDangKy benhNhan =
        //      GlobalCache.BenhNhanDangKyService.GetByBenhNhanId((int)dtrDuyetCapThe["BenhNhanId"]);
            
        //    //Duyet & Khong Duyet
        //    if ((int)dtrDuyetCapThe["Duyet"] == 1 || (int)dtrDuyetCapThe["Duyet"] == 2)
        //    {
        //      benhNhan.TrangThaiId = (int)dtrDuyetCapThe["Duyet"];
        //      benhNhan.GhiChu = dtrDuyetCapThe["GhiChu"] as string;
        //      benhNhan.UserDuyet = GlobalCache.Permission.Username;
        //      benhNhan.NgayDuyet = DateTime.Today;
        //    }

        //    GlobalCache.BenhNhanDangKyService.Save(benhNhan);
        //  }

        //  e.Result = ConnectionScope.Complete();
        //}
      }
      catch (Exception ex)
      {
        Logger.LogCritical("FrmDsDuyetCapThe::btnOK_Click", ex, "General", null);
        e.Result = ex;
      }
    }

    private void bwDataSaving_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      Cursor = Cursors.Default;
      MainFormUtils.StopProgress();

      if (e.Result is Exception)
      {
        MainFormUtils.SetSavedFailedStatus();
        UIUtils.ShowCannotSaveDataMessage();
      }
      else if (e.Result is bool)
      {
        if (!(bool)e.Result)
        {
          MainFormUtils.SetSavedFailedStatus();
          UIUtils.ShowErrorMessage(Constants.MsgErrDuyetCapThe_001);
        }
        else
        {
          MainFormUtils.SetSavedStatus();
          LoadData();
        }
      }
      else
      {  
        MainFormUtils.SetDefaultStatus();
      }
    }

    #endregion

    #endregion

    #region Helper Methods

    private void LoadData()
    {
      Cursor = Cursors.WaitCursor;
      MainFormUtils.StartProgress();
      MainFormUtils.SetLoadingStatus();
      bwDataLoading.RunWorkerAsync();
    }

    #endregion

  }
}